/* 
 * Tester.java
 *
 * Created on February 19, 2007, 7:10 PM
 *
 * This class tests all funtionallity in this programs DAO's and BO's.
 */

package edu.byu.isys413.jjenkins.data;

import java.sql.Statement;
import java.sql.Connection;
import java.sql.Date;
import java.util.List;



/**
 * This class tests all funtionallity in this programs DAO's and BO's.
 * @author Jeff
 * This class tests all funtionallity in this programs DAO's and BO's.
 */
public class Tester {
    
    /** Creates a new instance of Tester */
    public Tester() {
        
    }
    /*test the CRUD functions the project*/
    /**
     * main
     * @param args ars
     */
    public static void main(String[] args){
        try{
            
            
            // clear out the database (you'd never do this in production)
            Connection conn = ConnectionPool.getInstance().get();
            Statement stmt = conn.createStatement();
            stmt.executeUpdate("DELETE FROM membership_interest");
            stmt.executeUpdate("DELETE FROM membership");
            stmt.executeUpdate("DELETE FROM customer");
            stmt.executeUpdate("DELETE FROM area_of_Interest");
            stmt.close();
            conn.commit();
            ConnectionPool.getInstance().release(conn);
            
            
            
            /**************************CustomerBO and DAO******************************/
            
            
            //Create a customer
            System.out.println(" \n\nCREATING CUST1");
            CustomerBO cust1 = CustomerDAO.getInstance().create();
            cust1.setFirstName("Homer");
            cust1.setLastName("Simpson");
            cust1.setEmail("dogs@ddd.com");
            cust1.setAddress("742 Evergreen Terrace");
            cust1.setCity("Springfield");
            cust1.setState("Unknown");
            cust1.setZip("84235");
            cust1.setPhone("801-224-0234");
            CustomerDAO.getInstance().save(cust1);
            
            //Create a second customer
            System.out.println(" \n\nCREATING CUST2");
            CustomerBO cust2 = CustomerDAO.getInstance().create();
            cust2.setFirstName("Marge");
            cust2.setLastName("Simpson");
            cust2.setEmail("pigs@dude.com");
            cust2.setAddress("742 Evergree Terrace");
            cust2.setCity("Unknown");
            cust2.setZip("84602");
            cust2.setPhone("801-224-0233");
            CustomerDAO.getInstance().save(cust2);;
            
            //Get Customer from Cache
            System.out.println(" \n\nGetting Customer 1 from Cache");
            CustomerBO cust3 = CustomerDAO.getInstance().read(cust1.getId());
            System.out.println("Item in Cache: " + cust3.getId() + " " + cust3.getFirstName() + " " + cust3.getEmail());
            
            
            
            // reload everything from scratch to test whether the read methods work
            // this essentially is like restarting the entire system since
            // we're going to clear the cache out
            System.out.println("\n\nGetting Customer 2 from DB id =  " + cust2.getId());
            
            Cache.getInstance().clear(); // you'd never clear the cache in production (we do so for testing)
            CustomerBO cust4 = CustomerDAO.getInstance().read(cust2.getId());
            System.out.println("Item in DB: " + cust4.getId() + " " + cust4.getFirstName() + " " + cust4.getEmail());
            
            
            // Update an object
            System.out.println(" \n\nUpdating cust 2 in cache and database...");
            cust4.setFirstName("Testing");
            CustomerDAO.getInstance().save(cust4);
            
            //Display that object
            System.out.println(" \n\nGetting Updated Customer 2 from DB");
            Cache.getInstance().clear(); // you'd never clear the cache in production (we do so for testing)
            CustomerBO cust6 = CustomerDAO.getInstance().read(cust2.getId());
            System.out.println("Item in DB: " + cust6.getId() + " " + cust6.getFirstName() + " " + cust6.getEmail());
            
            //Get all Customers
            System.out.println(" \n\nGetting List of all customers");
            List li = CustomerDAO.getInstance().getAll();
            for(int i = 0; i<li.size(); i++){
                System.out.println(((String[])li.get(i))[1]);
            }
            
            //Get customers by phone
            System.out.println(" \n\nGetting List of all customers by phone");
            List li2 = CustomerDAO.getInstance().getByPhone("801-224-0233");
            for(int i = 0; i<li2.size(); i++){
                System.out.println(((CustomerBO)li2.get(i)).getFirstName());
            }
            
            //Get customers by name
            System.out.println(" \n\nGetting List of all customers by name");
            List li3 = CustomerDAO.getInstance().getByName("Homer", "Simpson");
            for(int i = 0; i<li3.size(); i++){
                System.out.println(((CustomerBO)li3.get(i)).getFirstName());
            }
            
            
            /********************Membership BO and DAO (and Membership Areas of Intersest***********/
            
            //Create a member
            System.out.println(" \n\nCREATING Member");
            MemberBO mem1 = MembershipDAO.getInstance().create();
            cust1.setMember(mem1);
            mem1.setCustomer(cust1);
            mem1.setCreditCard("123412345");
            mem1.setStartDate(new Date(System.currentTimeMillis()));
            mem1.setExpireDate(new Date(System.currentTimeMillis()));
            MembershipDAO.getInstance().save(mem1);
            
            
            //Create a second member
            System.out.println(" \n\nCREATING Second Member");
            MemberBO mem11 = MembershipDAO.getInstance().create();
            cust2.setMember(mem11);
            mem11.setCustomer(cust2);
            mem11.setCreditCard("123412345");
            mem11.setStartDate(new Date(System.currentTimeMillis()));
            mem11.setExpireDate(new Date(System.currentTimeMillis()));
            MembershipDAO.getInstance().save(mem11);
            
            
            
            
            
            
            //Read Customer and Membership from Cache
            System.out.println(" \n\nReading Customer and Membership");
            System.out.println("Item in Cache: " + cust1.getId() + " " + cust1.getFirstName() + " " + cust1.getMember().getCreditCard());
            
            //Read Customer and Membership from Database
            System.out.println("\n\nGetting Customer and Membership from DB");
            Cache.getInstance().clear(); // you'd never clear the cache in production (we do so for testing)
            cust1 = CustomerDAO.getInstance().read(cust1.getId());
            System.out.println(cust1.getMember());
            System.out.println("Item in DB: " + cust1.getId() + " " + cust1.getFirstName() + " " + cust1.getMember().getCreditCard());
            
            
            // Update an membership and customer object
            System.out.println(" \n\nUpdating cust 1 in cache and database...");
            cust1.setFirstName("The Moo Cow");
            System.out.println(cust1.getMember());
            cust1.getMember().setCreditCard("999999999");
            CustomerDAO.getInstance().save(cust1);
            //Display that object
            System.out.println(" \n\nGetting Updated Customer and Member 1 from DB");
            Cache.getInstance().clear(); // you'd never clear the cache in production (we do so for testing)
            CustomerBO cust9 = CustomerDAO.getInstance().read(cust1.getId());
            System.out.println("Item in DB: " + cust9.getId() + " " + cust9.getFirstName() + " " + cust9.getMember().getCreditCard());
            
            
            // Read Membership Directly
            Cache.getInstance().clear(); // you'd never clear the cache in production (we do so for testing)
            System.out.println(" \n\nGetting mem1 directly from DB");
            MemberBO mem2 = MembershipDAO.getInstance().read(mem1.getId());
            System.out.println("Item in DB: " + mem2.getId() + " " + mem2.getCreditCard());
            
            //Read Membership Directly
            System.out.println(" \n\nGetting mem1 directly from cache");
            MemberBO mem3 = MembershipDAO.getInstance().read(mem1.getId());
            System.out.println("Item in Cache: " + mem3.getId() + " " + mem3.getCreditCard());
            
            
            // Update an membership directly
            System.out.println(" \n\nUpdating mem1 directly...");
            mem1.setCreditCard("808888888");
            MembershipDAO.getInstance().save(mem1);
            //Display that object
            System.out.println(" \n\nGetting Updated Member from DB");
            Cache.getInstance().clear(); // you'd never clear the cache in production (we do so for testing)
            MemberBO mem4 = MembershipDAO.getInstance().read(mem1.getId());
            System.out.println("Item in DB: " + mem4.getId() + " " + mem4.getCreditCard());
            
            
            /**************************InterestBO and DAO******************************/
            
            
            //Create a area of interest
            System.out.println(" \n\nCREATING Interest");
            InterestBO int1 = InterestDAO.getInstance().create();
            int1.setDescription("MP3 Players");
            
            
            cust1.getMember().setAreaOfInterest(int1.getId());
            InterestDAO.getInstance().save(int1);
            System.out.println("saved new interest");
            CustomerDAO.getInstance().save(cust1);
            
            //Create a second area of interest
            System.out.println(" \n\nCREATING Interest");
            InterestBO int2 = InterestDAO.getInstance().create();
            int2.setDescription("Video Players");
            cust1.getMember().setAreaOfInterest(int2.getId());
            cust2.getMember().setAreaOfInterest(int2.getId());
            InterestDAO.getInstance().save(int2);
            CustomerDAO.getInstance().save(cust1);
            System.out.println(cust2.isObjectAlreadyInDB());
            CustomerDAO.getInstance().save(cust2);
            
            
            
            //Get Customer from Cache
            System.out.println(" \n\nGetting Customer 1 with Areas of Interestfrom Cache");
            cust1 = CustomerDAO.getInstance().read(cust1.getId());
            System.out.println(cust1.getMember().getAreasOfInterest().size());
            System.out.println("Item in Cache: " + cust1.getFirstName() + " " + cust1.getMember().getCreditCard() + " Areas of Interest:" + InterestDAO.getInstance().read((String)cust1.getMember().getAreasOfInterest().get(0)).getDescription() + " " + InterestDAO.getInstance().read((String)cust1.getMember().getAreasOfInterest().get(1)).getDescription());
            
            
            
            // reload everything from scratch to test whether the read methods work
            // this essentially is like restarting the entire system since
            // we're going to clear the cache out
            System.out.println("\n\nGetting Customer 2 with Areas of Intersts from DB id =  " + cust2.getId());
            
            Cache.getInstance().clear(); // you'd never clear the cache in production (we do so for testing)
            cust2 = CustomerDAO.getInstance().read(cust2.getId());
            System.out.println("Item in Cache: " + cust2.getFirstName() + " " + cust2.getMember().getCreditCard() + " Areas of Interest:" + InterestDAO.getInstance().read((String)cust2.getMember().getAreasOfInterest().get(0)).getDescription());
            
            
            // Update an object
            System.out.println(" \n\nUpdating interest 1 in cache and database...");
            int1.setDescription("mp4444 player");
            InterestDAO.getInstance().save(int1);
            
            //Display that object
            System.out.println(" \n\nGetting Updated Customer 1 with Interests from DB");
            Cache.getInstance().clear(); // you'd never clear the cache in production (we do so for testing)
            cust1 = CustomerDAO.getInstance().read(cust1.getId());
            System.out.println("Item in Cache: " + cust1.getFirstName() + " " + cust1.getMember().getCreditCard() + " Areas of Interest:" + InterestDAO.getInstance().read((String)cust1.getMember().getAreasOfInterest().get(0)).getDescription() + " " + InterestDAO.getInstance().read((String)cust1.getMember().getAreasOfInterest().get(1)).getDescription());
            
            //Get all Interests
            System.out.println(" \n\nGetting List of all interests");
            List li7 = InterestDAO.getInstance().getAll();
            for(int i = 0; i<li7.size(); i++){
                System.out.println(((InterestBO)li7.get(i)).getId() + " " + ((InterestBO)li7.get(i)).getDescription());
            }
            
            //Get customers by name
            System.out.println(" \n\nGetting List of all interest by name");
            List li9 = InterestDAO.getInstance().getByName("Video Players");
            for(int i = 0; i<li9.size(); i++){
                System.out.println(((InterestBO)li9.get(i)).getDescription());
            }
            
            
            
            
        }catch(Exception e){
            System.out.println(e);
            e.printStackTrace();
        }
    }
    
    
    
    
}

